package com.example.aigc_education.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.azure.core.annotation.Post;
import com.example.aigc_education.constant.WeChatConstants;
import com.example.aigc_education.domain.request.SendPlanSuccessRequest;
import com.example.aigc_education.domain.request.SendTaskSuccessRequest;
import com.example.aigc_education.domain.request.WechatRegisterRequest;
import com.example.aigc_education.domain.vo.BindSuccessVO;
import com.example.aigc_education.domain.vo.IdVO;
import com.example.aigc_education.service.UserService;
import com.example.aigc_education.service.WeChatService;
import com.example.aigc_education.service.impl.WeChatServiceImpl;
import com.example.aigc_education.utils.HttpClientUtil;
import com.example.aigc_education.utils.ResultUtil;
import com.example.aigc_education.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 微信小程序接口
 */
@Slf4j
@RestController
@RequestMapping("/wechat")
public class WeChatContrller {


    @Autowired
    private WeChatService weChatService;

    @Autowired
    private UserService userService;

    /**
     * 通过微信登录
     *
     * @param wechatCode 微信登录code
     * @return 返回登录结果，其中包含token
     * @RequestBody LoginByWeChatDTO loginByWeChatDTO
     */

    @PostMapping("/login")
    public ResultUtil loginByWeChat(String wechatCode) {
        log.info("微信用户登录：{}", wechatCode);
        ResultUtil result = ResultUtil.success(); // 初始化登录成功结果
        String token = weChatService.loginByWeChat(wechatCode); // 执行微信登录
        result.put("token", token); // 将生成的token添加到结果中
        return result;
    }


    /**
     * 微信绑定
     *
     * @param wechatCode 微信登录code
     * @return 返回绑定结果，其中包含userid和openId
     */
    @PostMapping("/bind")
    public ResultUtil bindWithWeChat(String wechatCode) {
        log.info("微信用户绑定：{}", wechatCode);
        BindSuccessVO result = weChatService.bindWithWeChat(wechatCode); // 执行微信绑定
        return ResultUtil.success("success", result);
    }

    /**
     * 微信解绑
     *
     * @return 返回解绑结果，其中包含userid
     */
    @PostMapping("/unbind")
    public ResultUtil UnBindWithWeChat() {
        IdVO result = weChatService.unBindWithWeChat(); // 执行微信绑定
        return ResultUtil.success("success", result);
    }


    /**
     * 发送计划生成成功的消息
     *
     * @param request 包含planId和toUrl的请求体
     * @return 发送结果
     */
    @PostMapping("/PlanGenMsg")
    public ResultUtil sendPlanSuccessMessage(@RequestBody SendPlanSuccessRequest request) {
        boolean success = weChatService.sendGenPlanSuccessMsg(request.getPlanId(), request.getToUrl());
        return success ? ResultUtil.success() : ResultUtil.error("发送失败");
    }

    /**
     * 发送任务生成成功的消息
     *
     * @param request 包含executeId、任务类型和toUrl的请求体
     * @return 发送结果
     */
    @PostMapping("/TaskGenMsg")
    public ResultUtil sendTaskStatusMessage(@RequestBody SendTaskSuccessRequest request) {
        boolean success = weChatService.sendGenTaskStatusMsg(request.getExecuteId(), request.getTaskType(), request.getToUrl());
        return success ? ResultUtil.success() : ResultUtil.error("发送失败");
    }

    @PostMapping("/register")
    public ResultUtil register(@RequestBody WechatRegisterRequest request) {
        String openid = weChatService.getOpenid(request.getWechatCode());
        String username = request.getUsername();
        String password = request.getPassword();
        Boolean success = userService.registerByOpenId(username, password, openid);
        return ResultUtil.success("success");
    }


}
